package com.example.tianling.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.tianling.common.annotation.Login;
import com.example.tianling.common.annotation.LoginUser;
import com.example.tianling.common.utils.BigModelNew;
import com.example.tianling.common.utils.R;
import com.example.tianling.common.utils.fileToWord;
import com.example.tianling.dao.MaterialMapper;
import com.example.tianling.entity.BookEntity;
import com.example.tianling.entity.MaterialEntity;
import com.example.tianling.entity.UserEntity;
import com.example.tianling.service.BookService;
import com.example.tianling.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Controller
public class MaterialController {
    @Autowired
    private MaterialService materialService ;//使用RabbiTeme,提供了接受发送方法
    //获取类型
    @Login
    @PostMapping(value="/getMaterialsType")
    @ResponseBody
    public R getMaterialsType(@RequestBody String params) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String region = paramsJSONObject.getString("region");
        return R.success(materialService.getMaterialsType(region));
    }

    @Login
    @PostMapping(value="/material/searchMatrial.do")
    @ResponseBody
    public R searchMatrial(@RequestBody String params,@LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String keyword = paramsJSONObject.getString("keyword");
        String page = paramsJSONObject.getString("page");
        return R.success(materialService.searchMaterial(Integer.parseInt(page),keyword));
    }

    @Login
    @PostMapping(value="/material/getMatrial.do")
    @ResponseBody
    public R getMatrial(@RequestBody String params,@LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String region = paramsJSONObject.getString("region");
        String materialType = paramsJSONObject.getString("materialType");
        String page = paramsJSONObject.getString("page");
        String keyword = paramsJSONObject.getString("keyword");
        return R.success(materialService.getMaterial(region,materialType, Integer.parseInt(page),keyword));
    }

    @Login
    @PostMapping(value="/material/getMatrialDetail.do")
    @ResponseBody
    public R getMatrialDetail(@RequestBody String params,@LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String id = paramsJSONObject.getString("id");
        return R.success(materialService.getMaterialsDetail(Integer.valueOf(id)));
    }

    @Login
    @PostMapping(value="/material/getMatrialAnalysis.do")
    @ResponseBody
    public R getMatrialAnalysis(@RequestBody String params,@LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String id = paramsJSONObject.getString("id");
        return R.success(materialService.getAnalysis(Integer.valueOf(id)));
    }
    @Login
    @PostMapping(value="/material/nextMaterial.do")
    @ResponseBody
    public R nextMaterial(@RequestBody String params, @LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String region = paramsJSONObject.getString("region");
        String materialType = paramsJSONObject.getString("materialType");
        String keyword = paramsJSONObject.getString("keyword");
        String id = paramsJSONObject.getString("id");
        MaterialEntity materialEntity  = materialService.nextMaterial(region,materialType, Integer.valueOf(id),keyword);
        if(materialEntity==null)
        {
            return R.error(409,"素材已到底");
        }
        else{
            return R.success(materialEntity);
        }
    }

    @Login
    @PostMapping(value="/material/lastMaterial.do")
    @ResponseBody
    public R lastBook(@RequestBody String params, @LoginUser UserEntity user) {
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String region = paramsJSONObject.getString("region");
        String materialType = paramsJSONObject.getString("materialType");
        String keyword = paramsJSONObject.getString("keyword");
        String id = paramsJSONObject.getString("id");
        MaterialEntity materialEntity  = materialService.lastMaterial(region,materialType, Integer.valueOf(id),keyword);
        if(materialEntity==null)
        {
            return R.error(409,"素材已到底");
        }
        else{
            return R.success(materialEntity);
        }
    }
}
